package com.example.hp.yuejian5.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hp.yuejian5.R;
import com.example.hp.yuejian5.RetrofitListener.RetrofitListener;
import com.example.hp.yuejian5.bean.AttentionBean;
import com.example.hp.yuejian5.bean.IsAttentionBean;
import com.example.hp.yuejian5.bean.MasterBean;
import com.example.hp.yuejian5.model.AttentionMasterModel;
import com.example.hp.yuejian5.model.CancelAttentionMasterModel;
import com.example.hp.yuejian5.model.IsAttentionMasterModel;
import com.example.hp.yuejian5.model.Master_DetailModel;
import com.squareup.picasso.Picasso;

import static com.example.hp.yuejian5.other.url.IMAGES;

public class Master_DetailActivity extends AppCompatActivity {
    private String i_id, u_id;
    private Context context;
    private RetrofitListener<MasterBean> listener;

    private ImageView master_detail_pic;
    private int flagattention;
    private ImageView isattentionmaster;
    private SharedPreferences sp;
    private TextView master_detail_name,master_detail_nickname,master_detail_attention,master_detail_intro;
    private RetrofitListener<IsAttentionBean> isattentionBeanRetrofitListener;
    private RetrofitListener<AttentionBean> attentionBeanRetrofitListener, cancelattentionBeanRetrofitListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_master_detail);
        Intent intent = getIntent();
        i_id = (String) intent.getStringExtra("m_id");
        sp = getSharedPreferences("user_id", Context.MODE_PRIVATE);
        u_id = sp.getString("userid", null);
        initView();
        initEvents();
        listener = new RetrofitListener<MasterBean>() {
            @Override
            public void onSuccess(MasterBean masterBean, int flag) {
                master_detail_name.setText(masterBean.getMaster_name());
                master_detail_nickname.setText(masterBean.getMaster_nickname());
                master_detail_attention.setText(masterBean.getMaster_attention());
                master_detail_intro.setText(masterBean.getMaster_intro());
                Picasso.with(context).load(IMAGES + masterBean.getMaster_pic()).into(master_detail_pic);
            }

            @Override
            public void onFail() {

            }
        };

        isattentionBeanRetrofitListener = new RetrofitListener<IsAttentionBean>() {
            @Override
            public void onSuccess(IsAttentionBean isAttentionBean, int flag) {
                flagattention = Integer.parseInt(isAttentionBean.getIsattention());
                if (isAttentionBean.getIsattention().equals("1")) {
                   isattentionmaster.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.aaa));
                }

            }

            @Override
            public void onFail() {

            }
        };

        attentionBeanRetrofitListener = new RetrofitListener<AttentionBean>() {
            @Override
            public void onSuccess(AttentionBean attentionBean, int flag) {
                if (attentionBean.getSuccess().equals("1")) {
                    flagattention = 1;
                    Toast.makeText(Master_DetailActivity.this, "关注成功", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFail() {

            }
        };

        cancelattentionBeanRetrofitListener = new RetrofitListener<AttentionBean>() {
            @Override
            public void onSuccess(AttentionBean attentionBean, int flag) {
                if (attentionBean.getSuccess().equals("1")) {
                    flagattention = 0;
                    Toast.makeText(Master_DetailActivity.this, "取消关注成功", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFail() {

            }
        };

        showDetail();
        isAttentionMaster();
    }

    private void initView() {
        master_detail_pic= findViewById(R.id.master_detail_pic);
        master_detail_name = findViewById(R.id.master_detail_name);
        master_detail_nickname = findViewById(R.id.master_detail_nickname);
        master_detail_attention= findViewById(R.id.master_detail_attention);
        master_detail_intro= findViewById(R.id.master_detail_intro);
        isattentionmaster=findViewById(R.id.isattrntionmaster);
    }

    private void initEvents() {

        isattentionmaster.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flagattention == 1) {
                    cancelAttentionMaster();
                    isattentionmaster.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.bbb));
                } else {
                    attentionMaster();
                    isattentionmaster.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.aaa));

                }
            }
        });
    }
    private void showDetail() {
        Master_DetailModel master_detailModel = new Master_DetailModel();
        master_detailModel.masterDetail(i_id, listener);
    }
    private void isAttentionMaster() {
        IsAttentionMasterModel isAttentionMasterModel = new IsAttentionMasterModel();
        isAttentionMasterModel.isAttentionmaster(u_id, i_id, isattentionBeanRetrofitListener);
    }

    private void cancelAttentionMaster() {
        CancelAttentionMasterModel cancelAttentionMasterModel = new CancelAttentionMasterModel();
        cancelAttentionMasterModel.cancelAttentionMaster(u_id, i_id, cancelattentionBeanRetrofitListener);
    }

    private void attentionMaster() {
        AttentionMasterModel attentionMasterModel = new AttentionMasterModel();
        attentionMasterModel.attentionMaster(u_id, i_id, attentionBeanRetrofitListener);
    }


}
